/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.jm.financeiro.filter;

import br.jm.financeiro.base.eao.EaoException;
import br.jm.financeiro.entity.Cliente;
import br.jm.financeiro.sistema.eao.ClienteEao;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author joao
 */
public class ClienteConverter implements Converter {
    private final ClienteEao ClienteJpa;
    private Cliente selectedCliente;
    
    public ClienteConverter() throws EaoException {
        ClienteJpa = ClienteEao.getInstance();
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        int number = Integer.parseInt(value);
        try {
            selectedCliente = ClienteJpa.getPorId(number);
        } catch (Exception ex) {
            selectedCliente = null;
        }
        return selectedCliente;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null || value.toString().trim().equals("")) {
            return "";
        } else {
            return String.valueOf(((Cliente) value).getIdCliente());
        }
    }
    
}
